package mouseAndKeyAndScroll;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

// 双击事件
public class MouseDemo extends Application {
	private Pane root = new Pane();
	private Button button;
	private double innerX;
	private double innerY;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		button = new Button("click me");
		button.setLayoutX(20);
		button.setLayoutY(150);
		root.getChildren().addAll(button);
		
		Scene scene = new Scene(root, 350, 300);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		// 鼠标左键双击事件
		button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				if(event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY ) {
					System.out.println(event.getButton() + "  双击事件");
				}
			}
		});
		
		// 鼠标左键双击事件
//		button.setOnMouseClicked(e -> {
//			if(e.getClickCount() == 2 && e.getButton() == MouseButton.PRIMARY) {
//				System.out.println(e.getButton() + "双击事件");
//			}
//		});
		
		// 鼠标拖拽时位于图形中间(要分开获取)
		// 注意e.getSceneX()与e.getX()的区别
		button.setOnMousePressed(e -> {
			innerX = e.getX();
			innerY = e.getY();
		});
				
		button.setOnMouseDragged(e -> {
			button.setLayoutX(e.getSceneX() - innerX);
			button.setLayoutY(e.getSceneY() - innerY);
		});
				
		// 鼠标移动(Pane中移动)
//		root.setOnMouseMoved(e -> System.out.println("move") );
//		button.setMouseTransparent(true);	鼠标事件变透明
	}
}
